package com.aj.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aj.activity.HomesMarryActivity;
import com.aj.activity.HomesMemoryActivity;
import com.aj.activity.HomesRelationActivity;
import com.aj.activity.HomesRelationGesturePasswordActivity;
import com.aj.activity.R;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.AccountDao;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.Account;
import com.aj.databases.entry.User;
import com.aj.sharepreferences.LoveHomeSharedPreferences;
import com.aj.utils.Contants;

/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment implements OnClickListener {

	private ImageView homes_memory;
	private ImageView homes_relation;
	private ImageView homes_memorial_day;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_home, container, false);
		homes_memory = (ImageView) view.findViewById(R.id.homes_memory);
		homes_memory.setOnClickListener(this);
		homes_relation = (ImageView) view.findViewById(R.id.homes_relation);
		homes_relation.setOnClickListener(this);

		homes_memorial_day = (ImageView) view
				.findViewById(R.id.homes_memorial_day);
		homes_memorial_day.setOnClickListener(this);
		setMemorialEnable();

		return view;
	}

	private void setMemorialEnable() {
		boolean isMemoried = false;
		if (LoveHomeApplication.token != null
				&& LoveHomeApplication.token.trim().length() > 0) {
			AccountDao accountDao = new AccountDao(getActivity());
			UserDao userDao = new UserDao(getActivity());
			Account account = accountDao.getByToken(LoveHomeApplication.token);
			if (account != null) {
				User user = userDao.getUserByUserId(account
						.getLast_used_user_id());
				if (user != null) {
					if ("0".equals(user.getIsMarried())
							|| "2".equals(user.getIsMarried())) {
						isMemoried = true;
					} else {
						isMemoried = false;
					}

					homes_memorial_day.setEnabled(isMemoried);

					if (isMemoried) {
						homes_memorial_day
								.setBackgroundResource(R.drawable.wedding_anniversary_1);
					} else {
						homes_memorial_day
								.setBackgroundResource(R.drawable.wedding_anniversary_2);
					}
				}
			}
		}

	}

	@Override
	public void onClick(View v) {
		if (LoveHomeApplication.token == null
				|| LoveHomeApplication.token.length() == 0) {
			Intent intent = new Intent(Contants.LOGINACTIVITY_ACTION);
			startActivity(intent);
			return;
		}
		switch (v.getId()) {
		case R.id.homes_memory:
			HomesMemoryActivity.newInstance(getActivity());
			break;
		case R.id.homes_relation:
			String gesturePassword = LoveHomeSharedPreferences
					.getGesturePassowrd().trim();
			if (gesturePassword != null && !"".equals(gesturePassword)) {
				HomesRelationGesturePasswordActivity.newInstance(getActivity());
			} else {
				HomesRelationActivity.newInstance(getActivity());
			}
			break;
		case R.id.homes_memorial_day:
			HomesMarryActivity.newInstance(getActivity());
			break;

		default:
			break;
		}

	}

}
